package com.singlemusic.controller;

import java.io.File;
import java.io.InputStream;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.singlemusic.pojo.Artists;
import com.singlemusic.service.impl.ArtistService;
import com.singlemusic.utils.AESUtils;
import com.singlemusic.utils.FileUtils;
import com.singlemusic.utils.UuidUtils;

@Controller
@RequestMapping("/singer")
public class ArtistsController {
	@Resource(name="artistService")
	private ArtistService artistService;
	@RequestMapping("addSinger")
	public void addSinger(@ModelAttribute Artists artists, @RequestParam("file")MultipartFile file,
			HttpServletRequest request){
		String path = request.getSession().getServletContext().getRealPath("data");
		
		String saveFolderPath = path+"/"+artists.getArtistname()+"/ico";
		String fileName = file.getOriginalFilename();
		String savePath = "/"+artists.getArtistname()+"/ico/"+fileName;
		
		InputStream is = null;
		System.out.println(savePath);
		try {
			is = file.getInputStream();
		} catch (Exception e) {
			e.printStackTrace();
		}
		boolean flag = FileUtils.saveFile(is, saveFolderPath, fileName);
		if(flag=true){
			System.out.println("save success");
			artists.setArtistid(UuidUtils.getUuidCode());
			artists.setArtistico(AESUtils.enCrypt(savePath));
			boolean result = artistService.addSinger(artists);
			if(result=true){
				System.out.println("add success");
			}else{
				System.out.println("add failed");
			}
			
		}else{
			System.out.println("save failed");
		}
	}
}
